package com.todaynews.frame.okhttp;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Headers;

/**
 * Created by jack
 */

public abstract class OkHttpRequest<T> {

    private int readTimeOut = 25;//数据读取超时时间，默认为30s
    private int writeTimeOut = 25; //写超时时间，默认为15s
    private int connectTimeOut = 25; //连接超时时间，默认为25s
    private List<KeyValue> requestParams;//请求参数
    private int tag;//请求标识
    private String url;//请求地址
    private Map<String,String> headersParams;
    private EnumRequestWay enumRequestWay;//默认POST请求

    public OkHttpRequest(int tag, String url){
        requestParams = new ArrayList<>();
        headersParams = new HashMap<>();
        this.url = url;
        this.tag = tag;
        enumRequestWay = EnumRequestWay.POST;
    }

    /**
     * 设置Header头
     * @param headersParams
     * @return
     */
    private Headers setHeaders(Map<String,Object> headersParams){
        Headers headers = null;
        Headers.Builder headerBuilder = new Headers.Builder();
        if(headersParams != null){
            for (String key:headersParams.keySet()){
                headerBuilder.add(key,headersParams.get(key).toString());
            }
        }
        headers = headerBuilder.build();
        return headers;
    }

    /**
     * 设置get连接拼接参数
     * @param params
     * @return
     */
    private String setUrlParams(Map<String,Object> params){
        StringBuffer param = new StringBuffer();
        int i = 0;
        if(params == null){
            return param.toString();
        }
        for (String key : params.keySet()) {
            if (i == 0)
                param.append("?");
            else
                param.append("&");
            try {
                param.append(key).append("=").append(URLEncoder.encode(params.get(key).toString(),"UTF-8"));  //字符串拼接
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            i++;
        }
        return param.toString();
    }

    public void addHeaderParams(String key, String value) {
        headersParams.put(key,value);
    }

    public void addParams(String key, String value) {
        requestParams.add(new KeyValue(key, value));
    }

    public void addParams(String key, List value) {
        requestParams.add(new KeyValue(key, value));
    }

    public void addParams(String key, long value) {
        requestParams.add(new KeyValue(key, String.valueOf(value)));
    }

    public void addParams(String key, int value) {
        requestParams.add(new KeyValue(key, String.valueOf(value)));
    }

    public void addParams(String key, short value) {
        requestParams.add(new KeyValue(key, String.valueOf(value)));
    }

    public void addParams(String key, float value) {
        requestParams.add(new KeyValue(key, String.valueOf(value)));
    }

    public void addParams(String key, double value) {
        requestParams.add(new KeyValue(key, String.valueOf(value)));
    }

    public void addParams(String key, File value) {
        requestParams.add(new KeyValue(key, value));
    }

    public int getReadTimeOut() {
        return readTimeOut;
    }

    protected abstract T getResult(String responseBody);

    public void setReadTimeOut(int readTimeOut) {
        this.readTimeOut = readTimeOut;
    }

    public int getWriteTimeOut() {
        return writeTimeOut;
    }

    public void setWriteTimeOut(int writeTimeOut) {
        this.writeTimeOut = writeTimeOut;
    }

    public int getConnectTimeOut() {
        return connectTimeOut;
    }

    public void setConnectTimeOut(int connectTimeOut) {
        this.connectTimeOut = connectTimeOut;
    }

    public List<KeyValue> getRequestParams() {
        return requestParams;
    }

    public void setRequestParams(List<KeyValue> requestParams) {
        this.requestParams = requestParams;
    }

    public int getTag() {
        return tag;
    }

    public void setTag(int tag) {
        this.tag = tag;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Map<String, String> getHeadersParams() {
        return headersParams;
    }

    public void setHeadersParams(Map<String, String> headersParams) {
        this.headersParams = headersParams;
    }

    public enum EnumRequestWay {
        GET, POST
    }

    public EnumRequestWay getEnumRequestWay() {
        return enumRequestWay;
    }

    public void setEnumRequestWay(EnumRequestWay enumRequestWay) {
        this.enumRequestWay = enumRequestWay;
    }
}
